<template>
  <a-drawer title="成本价格列表" width="60%" :visible="visible" :confirmLoading="loading" @close="() => {
    this.visible = false
  }
    ">

    <a-form layout="inline">
      <a-row :gutter="10">
        <a-col :md="24" :sm="24" style="text-align:right;">          
          <a-form-item label="存货名称">
            <a-input v-model="keyword" placeholder="请输入存货名称" @keydown.enter="getDataList" />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="() => {
    this.pagination.current = 1
    this.getDataList()
  }
    ">查询
            </a-button>
          </a-form-item>

        </a-col>
      </a-row>
    </a-form>

    <a-spin :spinning="loading">
      <a-table ref="table" :scroll="{ y: 530 }" :columns="columns" :rowKey="(row, index) => row.Id" :dataSource="data"
        :total="total" :pagination="pagination" @change="handleTableChange" :bordered="true" size="small">

      </a-table>

    </a-spin>
    <div :style="{
    position: 'absolute',
    right: 0,
    bottom: 0,
    width: '100%',
    borderTop: '1px solid #e9e9e9',
    padding: '4px 16px',
    background: '#fff',
    textAlign: 'right',
    zIndex: 1
  }">
      <a-button type="primary" @click="close">确定</a-button>&nbsp;
      <a-button @click="close">取消</a-button>&nbsp;
    </div>
  </a-drawer>
</template>

<script>

const columns = [
  {
    title: '仓库',
    dataIndex: 'Warehouse'

  },
  {
    title: '存货编码',
    dataIndex: 'Code'

  },
  {
    title: '存货名称',
    dataIndex: 'Name'
  },
  {
    title: '规格型号',
    dataIndex: 'Spec'
  },
  {
    title: '主单位',
    dataIndex: 'Unit'
  },
  {
    title: '数量（主单位）',
    align: 'right',
    dataIndex: 'Qualitity'
  },
  {
    title: '成本价',
    align: 'right',
    dataIndex: 'Price'
  },
  {
    title: '成本金额',
    align: 'right',
    dataIndex: 'TotalMoney'
  }
]
export default {
  data() {
    return {
      keyword:'',
      columns,
      visible: false,
      loading: false,
      data: [],
      total: 0,
      pagination: {
        total: 0,
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      }
    }
  },
  methods: {
    close() {
      this.visible = false
      this.data = []
    },
    init(time) {
      this.pagination.current = 1
      this.time = time
      this.visible = true
      this.data = []
    },
    openForm(time) {
      this.init(time)
      this.getDataList()
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    getDataList() {
      this.data = []
      this.pagination.total = 0
      this.loading = true
      this.$http.post('/Tmp_Price/tmp_price/GetDataListByMoreCondition', {
        'PageIndex': this.pagination.current,
        'PageRows': this.pagination.pageSize,
        'SortField': 'WarehouseCode',
        'SortType': 'asc',
        'Search': [{
          'Condition': 'SMonth',
          'Keyword': this.time
        },{
          'Condition': 'Name',
          'Keyword': this.keyword
        }]
      }).then(res => {
        console.log(res)
        if (res.Success) {
          this.data = res.Data.map(item => {
            item.TotalMoney = this.numberFormats(item.TotalMoney)
            item.Price = this.numberFormats(item.Price)
            return item
          }) || []
          this.pagination.total = res.Total
          this.total = res.Total
          const pagination = { ...this.pagination }
          pagination.total = res.Total
          this.pagination = pagination
        }
      }).finally(() => {
        this.loading = false
      })
    },
    numberFormats(value) {
      if (!value && value != 0) {
        return ''
      }
      // 获取元素的文本内容
      const originalValue = parseFloat(Number(value).toFixed(2))
      // 使用toLocaleString方法将数字格式化为千分位表示
      let res = originalValue.toLocaleString()
      const match = res.match(/\.(\d+)/)
      if (match && match[1].length == 1) {
        // 如果找到匹配小数点后面的数字的长度加0   /'22.4'+0
        return res += '0'
      } else if (match && match[1].length == 0 || !match) {
        return res += '.00'
      } else {
        // 如果没有匹配小数部分长度，返回原数字。
        return res
      }
    }
  }
}
</script>
